#!/bin/bash

sqlitedb="/usr/lib64/nagios/plugins/SiteJobMon/remotemon.db"
updatejobstatinsqlite(){
  site=$1
  pool=$2
  jobid=$3
  jobstat=$4
  sqlite3 $sqlitedb "update samjobtask set jobstat=$jobstat where jobid=${jobid} and site='$site' and pool='$pool'" 
}
datetime=`date +%s`
export _condor_SEC_CLIENT_AUTHENTICATION_METHODS=CLAIMTOBE
for line in `sqlite3 $sqlitedb "select site,pool,jobid,jobstat from samjobtask where jobstat=4"`
do
  echo $line
  objresultfile="/usr/lib64/nagios/plugins/SiteJobMon/result.log"
  site_schedd=${line%%|*}
  infoother=${line#*|}
  site_cm=${infoother%%|*}
  infoother=${infoother#*|}
  job_id=${infoother%%|*}
  dbjobstat=${infoother#*|}
  logger "condor_transfer_data jobid:${job_id} -name ${site_schedd} -pool ${site_cm}"
  rm -f $objresultfile
  fetchresult=`condor_transfer_data ${job_id} -name ${site_schedd} -pool ${site_cm}`
  resultcontent=`cat $objresultfile`
  sitemonmsg="$0 sitesched:${site_schedd} sitecm:${site_cm} jobid:$job_id monresult:$resultcontent tm:$datetime"
  logger $sitemonmsg
  jobstat=100
  updatejobstatinsqlite ${site_schedd} ${site_cm} ${job_id} $jobstat
done
